<script setup>
import { ref } from "vue";
import { invoke } from "@tauri-apps/api/core";
import TitleBar from "./components/TitleBar.vue";

const greetMsg = ref("");
const name = ref("");
const activeTab = ref("dashboard");

async function greet() {
  // Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
  greetMsg.value = await invoke("greet", { name: name.value });
}

const menuItems = [
  { key: "dashboard", label: "仪表板", icon: "Monitor" },
  { key: "components", label: "组件示例", icon: "Grid" },
  { key: "forms", label: "表单", icon: "Edit" },
  { key: "tables", label: "表格", icon: "Grid" },
  { key: "settings", label: "设置", icon: "Setting" }
];
</script>

<template>
  <div class="app-container">
    <!-- 自定义标题栏 -->
    <TitleBar title="Tauri + Vue + Element Plus" />
    
    <!-- 主内容区域 -->
    <div class="main-layout">
      <!-- 侧边栏 -->
      <el-aside class="sidebar" width="250px">
        <div class="sidebar-header">
          <el-icon :size="24" color="#409eff">
            <Grid />
          </el-icon>
          <span class="sidebar-title">应用框架</span>
        </div>
        
        <el-menu
          :default-active="activeTab"
          class="sidebar-menu"
          @select="activeTab = $event"
        >
          <el-menu-item 
            v-for="item in menuItems" 
            :key="item.key" 
            :index="item.key"
          >
            <el-icon>
              <component :is="item.icon" />
            </el-icon>
            <span>{{ item.label }}</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <!-- 主内容区 -->
      <el-main class="main-content">
        <!-- 仪表板内容 -->
        <div v-if="activeTab === 'dashboard'" class="content-section">
          <el-page-header class="page-header">
            <template #content>
              <span class="page-title">仪表板</span>
            </template>
          </el-page-header>
          
          <el-row :gutter="20" class="dashboard-cards">
            <el-col :span="6">
              <el-card class="stat-card" shadow="hover">
                <div class="stat-content">
                  <el-icon class="stat-icon" :size="32" color="#409eff">
                    <User />
                  </el-icon>
                  <div class="stat-info">
                    <div class="stat-value">1,234</div>
                    <div class="stat-label">用户数量</div>
                  </div>
                </div>
              </el-card>
            </el-col>
            
            <el-col :span="6">
              <el-card class="stat-card" shadow="hover">
                <div class="stat-content">
                  <el-icon class="stat-icon" :size="32" color="#67c23a">
                    <Document />
                  </el-icon>
                  <div class="stat-info">
                    <div class="stat-value">856</div>
                    <div class="stat-label">文档数量</div>
                  </div>
                </div>
              </el-card>
            </el-col>
            
            <el-col :span="6">
              <el-card class="stat-card" shadow="hover">
                <div class="stat-content">
                  <el-icon class="stat-icon" :size="32" color="#e6a23c">
                    <Star />
                  </el-icon>
                  <div class="stat-info">
                    <div class="stat-value">4.8</div>
                    <div class="stat-label">平均评分</div>
                  </div>
                </div>
              </el-card>
            </el-col>
            
            <el-col :span="6">
              <el-card class="stat-card" shadow="hover">
                <div class="stat-content">
                  <el-icon class="stat-icon" :size="32" color="#f56c6c">
                    <Message />
                  </el-icon>
                  <div class="stat-info">
                    <div class="stat-value">23</div>
                    <div class="stat-label">新消息</div>
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
          
          <!-- Tauri测试区域 -->
          <el-card class="demo-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <span>Tauri API 测试</span>
                <el-icon><Setting /></el-icon>
              </div>
            </template>
            
            <el-form class="greet-form" @submit.prevent="greet">
              <el-form-item label="输入姓名:">
                <el-input 
                  v-model="name" 
                  placeholder="请输入您的姓名..." 
                  clearable
                  style="width: 300px;"
                >
                  <template #prefix>
                    <el-icon><User /></el-icon>
                  </template>
                </el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="greet" :icon="Message">
                  问候
                </el-button>
              </el-form-item>
            </el-form>
            
            <el-alert 
              v-if="greetMsg" 
              :title="greetMsg" 
              type="success" 
              :closable="false"
              show-icon
            />
          </el-card>
        </div>
        
        <!-- 组件示例内容 -->
        <div v-else-if="activeTab === 'components'" class="content-section">
          <el-page-header class="page-header">
            <template #content>
              <span class="page-title">组件示例</span>
            </template>
          </el-page-header>
          
          <el-row :gutter="20">
            <el-col :span="12">
              <el-card header="按钮组件" shadow="hover">
                <el-space wrap>
                  <el-button>默认按钮</el-button>
                  <el-button type="primary">主要按钮</el-button>
                  <el-button type="success">成功按钮</el-button>
                  <el-button type="info">信息按钮</el-button>
                  <el-button type="warning">警告按钮</el-button>
                  <el-button type="danger">危险按钮</el-button>
                </el-space>
              </el-card>
            </el-col>
            
            <el-col :span="12">
              <el-card header="图标组件" shadow="hover">
                <el-space wrap>
                  <el-icon :size="24" color="#409eff"><Edit /></el-icon>
                  <el-icon :size="24" color="#67c23a"><Check /></el-icon>
                  <el-icon :size="24" color="#e6a23c"><Warning /></el-icon>
                  <el-icon :size="24" color="#f56c6c"><Close /></el-icon>
                  <el-icon :size="24" color="#909399"><Question /></el-icon>
                </el-space>
              </el-card>
            </el-col>
          </el-row>
        </div>
        
        <!-- 其他标签页内容占位 -->
        <div v-else class="content-section">
          <el-page-header class="page-header">
            <template #content>
              <span class="page-title">{{ menuItems.find(item => item.key === activeTab)?.label }}</span>
            </template>
          </el-page-header>
          
          <el-result
            icon="info"
            title="功能开发中"
            sub-title="此功能正在开发中，敬请期待！"
          >
            <template #extra>
              <el-button type="primary" @click="activeTab = 'dashboard'">返回仪表板</el-button>
            </template>
          </el-result>
        </div>
      </el-main>
    </div>
  </div>
</template>

<style scoped>
.app-container {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  background-color: var(--el-bg-color-page);
}

.main-layout {
  height: calc(100vh - 40px);
  margin-top: 40px;
  display: flex;
}

.sidebar {
  background-color: var(--el-bg-color);
  border-right: 1px solid var(--el-border-color-light);
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

.sidebar-header {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  border-bottom: 1px solid var(--el-border-color-light);
  gap: 12px;
}

.sidebar-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--el-text-color-primary);
}

.sidebar-menu {
  border: none;
  height: calc(100% - 60px);
}

.main-content {
  flex: 1;
  padding: 20px;
  background-color: var(--el-bg-color-page);
  overflow-y: auto;
}

.content-section {
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  margin-bottom: 20px;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: var(--el-text-color-primary);
}

.dashboard-cards {
  margin-bottom: 20px;
}

.stat-card {
  cursor: pointer;
  transition: transform 0.2s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
}

.stat-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.stat-info {
  flex: 1;
}

.stat-value {
  font-size: 24px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  line-height: 1;
}

.stat-label {
  font-size: 14px;
  color: var(--el-text-color-regular);
  margin-top: 4px;
}

.demo-card {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.greet-form {
  display: flex;
  align-items: end;
  gap: 12px;
  margin-bottom: 20px;
}
</style>

<style>
/* 全局样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
  overflow: hidden;
}

#app {
  height: 100vh;
  width: 100vw;
}

/* Element Plus 自定义主题 */
:root {
  --el-color-primary: #409eff;
  --el-color-success: #67c23a;
  --el-color-warning: #e6a23c;
  --el-color-danger: #f56c6c;
  --el-color-info: #909399;
}

/* 暗色模式适配 */
@media (prefers-color-scheme: dark) {
  :root {
    --el-bg-color-page: #141414;
    --el-bg-color: #1d1d1d;
  }
}
</style>
